package com.bonc.excel.configuration;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringUtils implements ApplicationContextAware {
    @Autowired
    private static ApplicationContext ac;

    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        SpringUtils.ac = ac;
    }

    public static Object getBean(String n) {
        return ac.getBean(n);
    }

    public static Object getBean(Class c) {
        return ac.getBean(c);
    }

    public static <T> Map<String, T> beans(Class<T> beanClz) {
        try {
            return ac.getBeansOfType(beanClz);
        } catch (Exception e) {
            return null;
        }

    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        try {
            return ac.getBean(name, clazz);
        } catch (Exception e) {
            return null;
        }
    }
}